package com.loyal.loyalerp.basedata.permission.service.impl;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.basedata.action.mapper.GyActionMapper;
import com.loyal.loyalerp.basedata.action.model.GyAction;
import com.loyal.loyalerp.basedata.permission.mapper.GyPermissionMapper;
import com.loyal.loyalerp.basedata.permission.model.GyPermission;
import com.loyal.loyalerp.basedata.permission.service.PermissionService;
import com.loyal.loyalerp.mapper.GyRolePermissionMapper;
import com.loyal.loyalerp.system.mapper.GySystemMapper;
import com.loyal.loyalerp.system.model.GySystem;
import com.loyal.loyalerp.utils.ValidateUtil;

@Service
public class PermissionServiceImpl implements PermissionService {
	@Autowired
	GyPermissionMapper gyPermissionMapper;
	
	@Autowired
	GySystemMapper systemMapper;
	
	@Autowired
	GyActionMapper actionMapper;
	
	@Autowired
	GyRolePermissionMapper rolePermissionMapper;

	@Override
	public PageInfo<GyPermission> listPermission(Integer PageNum, Integer recordNum, String sidx, String sord) {
		Page<GyPermission> page = PageHelper.startPage(PageNum, recordNum);
		gyPermissionMapper.listSortedPermission(sidx,sord);
		
		
		PageInfo<GyPermission> pageInfo = new PageInfo<>(page);
		return pageInfo;
	}

	@Override
	public int editPermission(String oper, GyPermission permission) {
	
		//根据systemname得到systemcode
		GySystem system=systemMapper.selectSystemCodeBySystemName(permission.getSystemcode());
		String systemcode=system.getSystemcode();
		permission.setSystemcode(systemcode);
		
		//根据actionname得到actioncode
		GyAction action=actionMapper.selectActionCodeByActionName(permission.getActioncode());
		String actioncode=action.getActioncode();
		permission.setActioncode(actioncode);
		
		
		if (oper.equals("add")) {
			
			if(!ValidateUtil.validateAdd("gy_permission")) {
				return 600;
			}
			
			System.out.println(permission.getPermissioncode());
			String code=permission.getPermissioncode();
			String entityCode=permission.getEntitycode();
			code=entityCode+"_"+actioncode;
			permission.setPermissioncode(code);
			try {
				gyPermissionMapper.insertSelective(permission);
				return 200;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (oper.equals("del")) {
			if(!ValidateUtil.validateDel("gy_permission")) {
				return 600;
			}
			
			rolePermissionMapper.deleteByPermissionId(permission.getId());
			try {
				gyPermissionMapper.deleteByPrimaryKey(permission.getId());
				return 200;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (oper.equals("edit")) {
			
			if(!ValidateUtil.validateEdit("gy_permission")) {
				return 600;
			}
			
			try {
				gyPermissionMapper.updateByPrimaryKeySelective(permission);
				return 200;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return 500;
	}


	@Override
	public PageInfo<GyPermission> findPermission(Integer PageNum,Integer recordNum, String sord,String searchField, String searchString) {
		
		String searchString2=searchString;
		/*
		Page<GyPermission> page=null;
		PageInfo<GyPermission> pageInfo =null;
		*/
		/*
		//判断systemcode
		if("systemCode".equals(searchField)) {
			GySystem system=systemMapper.selectSystemCodeBySystemName(searchString);
			String systemcode=system.getSystemcode();
			searchField=systemcode;
			page = PageHelper.startPage(PageNum, recordNum);
			List<GyPermission> permissionList=gyPermissionMapper.findPermission(searchField, searchString);
			pageInfo = new PageInfo<>(page);
		}
		return pageInfo;
		*/
		/*
		//判断actioncode
		if("actionCode".equals(searchField)) {
			GyAction action=actionMapper.selectActionCodeByActionName(searchString);
			String actioncode=action.getActioncode();
			searchField=actioncode;
			page = PageHelper.startPage(PageNum, recordNum);
			gyPermissionMapper.findPermission(searchField, searchString);
			pageInfo = new PageInfo<>(page);
		}
		*/
		
		if("systemCode".equals(searchField)) {
			GySystem system=systemMapper.selectSystemCodeBySystemName(searchString);
			String systemcode=system.getSystemcode();
			searchString2=systemcode;
		}
		
		if("actionCode".equals(searchField)) {
			GyAction action=actionMapper.selectActionCodeByActionName(searchString);
			String actioncode=action.getActioncode();
			searchString2=actioncode;
		}
		
		Page<GyPermission> page = PageHelper.startPage(PageNum, recordNum);
		gyPermissionMapper.findPermission(searchField, searchString2);
		PageInfo<GyPermission> pageInfo = new PageInfo<>(page);
		
		
		return pageInfo;
		
	}

	@Override
	public List<GyPermission> listAll() {
		return gyPermissionMapper.listPermission();
	}

	@Test
	public int del(String actionCode) {
		return gyPermissionMapper.deleteByForeignKey("gy_company_edit");
	}

}
